package com.gy.zpchat.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMGroupMemberListQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.entity.ZIMGroupMemberQueryConfig;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.enums.MessageLoadType;
import com.gy.zpchat.ui.BaseActivity;
import com.gy.zpchat.ui.chat.group.adapter.GroupMemberListAdapter;

public class ZIMGroupMemberSelectActivity extends BaseActivity {

    private static final String TAG = "ZIMGroupMemberSelectActivity";
    private String groupID;
    private int QUERY_MEMBER_COUNT = 30;
    private int queryNextFlag;
    private MessageLoadType messageLoadType;
    public static final int SELECT_MEMBER_RESULT_CODE = 200;

    private GroupMemberListAdapter memberListAdapter = new GroupMemberListAdapter();

    public static void actionStart(Activity activity, String groupID,String unSelectUid) {
        Intent intent = new Intent(activity, ZIMGroupMemberSelectActivity.class);
        intent.putExtra("groupID", groupID);
        intent.putExtra("unSelectMemberUid", unSelectUid);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_group_member);
        groupID = getIntent().getStringExtra("groupID");

        memberListAdapter.unSelectMember = getIntent().getStringExtra("unSelectMemberUid");

        RecyclerView recyclerView = findViewById(R.id.rv_member);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(memberListAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == memberListAdapter.getItemCount()) {
                    if (messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_LOADING && messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA) {
                        queryMemberList();
                    }
                }
            }
        });

        queryMemberList();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void queryMemberList() {
        messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOADING;
        ZIMGroupMemberQueryConfig groupMemberQueryConfig = new ZIMGroupMemberQueryConfig();
        groupMemberQueryConfig.count = QUERY_MEMBER_COUNT;
        groupMemberQueryConfig.nextFlag = queryNextFlag;
        SDKManager.sharedInstance().queryGroupMemberList(groupID, groupMemberQueryConfig, new ZIMGroupMemberListQueriedCallback() {
            @Override
            public void onGroupMemberListQueried(String groupID, ArrayList<ZIMGroupMemberInfo> userList, int nextFlag, ZIMError errorInfo) {

                memberListAdapter.addAll(userList);
                queryNextFlag = nextFlag;
                if (nextFlag == 0) {
                    messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA;
                } else {
                    messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOAD_COMPLETE;
                }
            }
        });
    }

    public void ConfirmClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("members",memberListAdapter.selectedMemberList);
        intent.putExtra("select member",bundle);
        setResult(SELECT_MEMBER_RESULT_CODE, intent);
        finish();
    }
}
